﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows;
using BAL.EstRec;

namespace Nav3.UIExcel
{
    public class TablaExcel
    {
        public static DataTable ExcelTabla(string tabla)
        {           

            DataTable table = new DataTable();
            List<string[]> list = new List<string[]>();
            string[] lines = Regex.Split(tabla, "\r\n");
            for (int i = 0; i < lines.Length - 1; i++)
            {
                list.Add(lines[i].Split('\t'));
            }

            table = ConvertListToDataTable(list);
           
            return table;
        }
        static DataTable ConvertListToDataTable(List<string[]> list)
        {
            // New table.
            DataTable table = new DataTable();
            DataColumn column;
            DataRow row;
            column = new DataColumn();
            column.ColumnName = "CLIENTE";

            column.Caption = "pos";
            table.Columns.Add(column);

            column = new DataColumn();
            column.ColumnName = "RECURSOS";
            
            column.Caption = "Recursos";
            table.Columns.Add(column);

            column = new DataColumn();
            column.ColumnName = "CANTIDAD";
            column.Caption = "Valor";
            
            table.Columns.Add(column);
            // Get max columns.
            int columns = 0;
            foreach (var array in list)
            {

                if (array.Length > columns)
                {
                    columns = array.Length;
                }
            }

             //Add columns.
            //for (int i = 0; i < columns; i++)
            //{
            //    table.Columns.Add();
            //}

            // Add rows.
                foreach (var array in list)
                {

                    int linqCount1 = (from DataRow row2 in table.Rows where ((string)row2["CLIENTE"]).Equals(array[0].ToString()) && ((string)row2["RECURSOS"]).Equals(array[1].ToString()) select row2).Count();
                    if (linqCount1 == 0)
                        table.Rows.Add(array[0], array[1], array[2]);
                }
            
                DataView vista = new DataView(table);
                DataTable dtsindupl = vista.ToTable(true, "CLIENTE", "RECURSOS", "CANTIDAD");

                return dtsindupl;
            
        }
    }
}
